<template>
  <div class="container_width common_wrap basics_detail">
    <el-divider>标准详情</el-divider>
    <el-card class="box-card box-card1 mb-10">
      <h3 class="detail_title">
        {{ infoData.title }}
      </h3>
      <div class="detail_title_en">
        {{ infoData.enTitle }}
      </div>
      <div class="tag_wrap">
        <el-tag type="success">{{ infoData.standardStatus }}</el-tag>
      </div>
      <!-- 标准动态的详情不展示按钮 -->
      <div style="text-align: right" v-if="!trendsFlag">
        <el-button size="mini" type="primary" @click="downloadFile(infoData.downloadStatus)">
          下载
        </el-button>
        <el-button size="mini" type="primary" @click="goReadOnline(infoData.readOnlineStatus)"
          >在线阅读</el-button
        >
      </div>
    </el-card>
    <el-card class="box-card">
      <div class="directory_wrap">
        <div class="left_directory">目 录</div>
        <div class="right_directory">
          <div class="item">
            <p><a @click="changeHash('#base')">1. 基础信息</a></p>
            <p><a @click="changeHash('#info')">2. 适用范围</a></p>
            <p><a @click="changeHash('#situation')">3. 关联标准</a></p>
          </div>
          <div>
            <p><a @click="changeHash('#unit')">4. 起草单位</a></p>
            <p><a @click="changeHash('#user')">5. 起草人</a></p>
          </div>
        </div>
      </div>
      <div class="detail_subtitle" id="base">基础信息</div>

      <el-descriptions class="descriptions_wrap" :column="2" border>
        <el-descriptions-item label="标准号">{{
          infoData.standardNo
        }}</el-descriptions-item>
        <el-descriptions-item label="标准状态">{{
          infoData.standardStatus
        }}</el-descriptions-item>
        <el-descriptions-item label="发布日期">{{
          infoData.pubTime || infoData.publishDate
        }}</el-descriptions-item>
        <el-descriptions-item label="中国标准分类号">{{
          infoData.ccs
        }}</el-descriptions-item>
        <el-descriptions-item label="实施日期">{{
          infoData.impDate || infoData.implDate
        }}</el-descriptions-item>
        <el-descriptions-item label="国际标准分类号">{{
          infoData.ics
        }}</el-descriptions-item>
        <el-descriptions-item label="采用的标准"
          ><a
            @click="goDetail('/basicsearch', infoData.adoptStandard)"
            class="link_a"
            >{{ infoData.adoptStandard }}</a
          ></el-descriptions-item
        >
        <el-descriptions-item label="归口单位">{{
          infoData.focalUnit
        }}</el-descriptions-item>
        <el-descriptions-item label="发布部门">{{
          infoData.pubDept
        }}</el-descriptions-item>
        <el-descriptions-item label="提出部门">{{
          infoData.sueDept
        }}</el-descriptions-item>
      </el-descriptions>

      <div class="detail_subtitle" id="info">适用范围</div>
      <div
        class="detail_subinfo detail_subinfo1"
        v-html="infoData.ownerDept"
      ></div>

      <div class="detail_subtitle" id="situation">关联标准</div>
      <div class="detail_subinfo">
        <p>
          引用的标准：<a
            @click="goDetail('/basicsearch', item)"
            class="link_a"
            v-for="(item, index) in infoData.referStandard"
            :key="index"
            >{{ item }}</a
          >
        </p>
        <p>
          替代以下的标准:
          <a
            @click="goDetail('/basicsearch', item)"
            class="link_a"
            v-for="(item, index) in infoData.repalceStandard"
            :key="index"
            >{{ item }}</a
          >
        </p>
        <p>
          被替代的标准:
          <a
            @click="goDetail('/basicsearch', item)"
            class="link_a"
            v-for="(item, index) in infoData.repalcedStandard"
            :key="index"
            >{{ item }}</a
          >
        </p>
      </div>
      <div class="detail_subtitle" id="unit">起草单位</div>
      <div class="detail_subinfo">
        <p class="bus">
          <span v-for="item in infoData.draftsUnit" :key="item">{{
            item
          }}</span>
        </p>
      </div>
      <div class="detail_subtitle" id="user">起草人</div>
      <div class="detail_subinfo">
        <p class="user">
          <span v-for="item in infoData.draftsMan" :key="item"
            ><i class="el-icon-user-solid main_color"></i> {{ item }}</span
          >
        </p>
      </div>
    </el-card>
     
  </div>
</template>

<script>
import { Loading } from "element-ui";
import {
  standardProfile,
  downLoadNotify,
  readOnlineNotify,
  fileUrl
} from "@/api/search.js";
export default {
  name: "",
  data() {
    return {
      standardNo: "",
      infoData: {},
      trendsFlag: false,
      downloadLoading: null,
      downloadStatus:true,
      readOnlineStatus:true
    };
  },
  created() {
    if (this.$route.query.row) {
      this.infoData = JSON.parse(this.$route.query.row);
      this.standardNo = this.infoData.standardNo;
      this.filterData(1);
      this.trendsFlag = true;
    } else {
      this.standardNo = this.$route.query.no;
      this.getDetail();
    }
  },
  methods: {
    getDetail() {
      standardProfile({ standardNo: this.standardNo }).then((res) => {
        if (res.code == 200) {
          this.infoData = res.data;
          this.filterData();
        }
      });
    },
    filterData(type) {
      if (type == 1) {
        this.infoData.draftsMan = this.infoData.draftsMan
          ? this.infoData.draftsMan.split(" ")
          : [];
      } else {
        this.infoData.draftsMan = this.infoData.draftsMan
          ? this.infoData.draftsMan.split("、")
          : [];
      }
      this.infoData.draftsUnit = this.infoData.draftsUnit
        ? this.infoData.draftsUnit.split("、")
        : [];

      if (this.infoData.ownerDept) {
        this.infoData.ownerDept = this.infoData.ownerDept.replaceAll("", "");
        this.infoData.ownerDept = this.infoData.ownerDept.replaceAll("", "");
        this.infoData.ownerDept = this.infoData.ownerDept.replaceAll("", "");
      }

      this.infoData.referStandard = this.infoData.referStandard
        ? this.infoData.referStandard.split(",")
        : [];
      this.infoData.repalceStandard = this.infoData.repalceStandard
        ? this.infoData.repalceStandard.split(",")
        : [];
      this.infoData.repalcedStandard = this.infoData.repalcedStandard
        ? this.infoData.repalcedStandard.split(",")
        : [];
    },
    goDetail(path, no) {
      let routeData = this.$router.resolve({
        path,
        query: {
          search:no,
        },
      });
      window.open(routeData.href, "_blank");
    },
    changeHash(idName) {
      document.querySelector(idName).scrollIntoView(true);
    },
    goReadOnline(status) {
      if(!status || !this.readOnlineStatus){
        this.$message({
          message: "您的套餐余额不足！",
          type: 'warning'
        })
        return
      }
      this.fileUrl(0)
 
    },
     downloadFile(status) {
      if(!status || !this.downloadStatus){
        this.$message({
          message: "您的套餐余额不足！",
          type: 'warning'
        })
        return
      }
      this.fileUrl(1)
    },

    downLoadNotify() {
      downLoadNotify({
        standardNo: this.standardNo,
        title: this.infoData.title,
      }).then((res) => {
        if(res.code==200){
          this.downloadStatus = res.data
        }
      });
    },
    readOnlineNotify() {
      readOnlineNotify({
        standardNo: this.standardNo,
        title: this.infoData.title,
      }).then((res) => {
        if(res.code==200){
          this.readOnlineStatus = res.data
        }
      });
    },
    // 获取pdf链接
    fileUrl(type) {
       fileUrl({
        standardNo:this.standardNo
      }).then( async(res) => {
        if(res.code==200 && res.data!=null){
          if(type==0){//type==0 在线阅读  type==1 下载
            this.readOnlineNotify();
          let route = this.$router.resolve({ path: '/pdf'});
          sessionStorage.setItem('urlLink',res.data)
          window.open(route.href,'_blank');
          }else if(type==1){
            this.downloadLoading = Loading.service({
        lock: true,
        text: "正在下载中，请稍后...",
        background: "rgba(0, 0, 0, 0.3)",
      });
      try {
        let url = res.data;

        let response = await fetch(url);
        // 内容转变成blob地址
        let blob = await response.blob();
        // 创建隐藏的可下载链接
        let objectUrl = window.URL.createObjectURL(blob);
        let a = document.createElement("a");
        a.style.display = "none";
        //地址
        a.href = objectUrl;
        //修改文件名
        a.download = this.infoData.standardNo + ".pdf";
        // 触发点击
        document.body.appendChild(a);
        a.click();
        //移除
        URL.revokeObjectURL(a.href);
      } catch (err) {
        this.downloadLoading.close();
      } finally {
        this.downloadLoading.close();
        this.downLoadNotify();
      }
          }
          
        }else{
          this.$message({
          message: "暂无文本信息！",
          type: 'warning'
        })
        return
        }
      });
    },
  },
};
</script>

<style lang="scss">
.basics_detail {
  .detail_title {
    margin-bottom: 5px;
  }
  .detail_title_en {
    margin-bottom: 10px;
  }
  .tag_wrap {
    .el-tag {
      margin-right: 10px;
    }
  }
  .directory_wrap {
    height: 150px;
    padding: 0 30px;
    margin-bottom: 30px;
    display: flex;
    .left_directory {
      width: 80px;
      height: 150px;
      text-align: center;
      line-height: 150px;
      background-color: #f2f2f2f9;
      border-right: 1px solid #d5d4d4;
    }
    .right_directory {
      display: flex;
      width: 500px;
      padding: 20px 30px;
      line-height: 30px;
      color: #2f74d3;
      .item {
        width: 200px;
      }
    }
  }
  .el-steps {
    justify-content: center;
  }
  .el-step__title {
    font-size: 14px;
  }
  .descriptions_wrap {
    margin: 20px;
  }

  .el-descriptions-item__label.is-bordered-label {
    width: 150px;
  }
  .el-descriptions-item__content {
    width: 300px;
  }

  .detail_subinfo1 {
    padding-left: 50px;
    padding-right: 50px;
  }
  .box-card {
    .el-card__body {
      padding: 20px 30px;
    }
  }
  .box-card1 {
    .el-card__body {
      border-top: 10px solid #2f74d3;
    }
  }
  .detail_subtitle {
    font-weight: bold;
    margin: 30px 0 15px;
    border-left: 4px solid #2f74d3;
    padding-left: 15px;
  }
  .detail_subinfo {
    font-size: 14px;
    line-height: 28px;
    padding-bottom: 20px;
    border-bottom: 1px solid #d5d4d4;
    p {
      padding-left: 50px;
    }
    .bus span {
      display: inline-block;
      width: 350px;
    }
    .user span {
      display: inline-block;
      width: 250px;
      i {
        margin-right: 10px;
      }
    }
  }
}
</style>
